package tp.pr5.items;

import java.util.LinkedList;

public class ItemList extends LinkedList<Item>
{

	public ItemList(Item[] arrayItems) {
		for (int i=0;i<arrayItems.length;i++)
			this.add(arrayItems[i]);
	}
	public ItemList() {
		super();
	}
	private static final long serialVersionUID = 1L;

	public int searchItem(String idItem)
	{
		int index=-1;
		Item item=null;
		for (int i = 0; i < this.size(); i++)
		{
			item=this.get(i);
			if (item.getId()!=null && !item.getId().equals("")) {
			if (item.getId().equalsIgnoreCase(idItem))
			{
				index= i;
			}
		}
			else  {
				this.remove(i);
				i--;
			}
			
		}
		
		return index;
	}

	public boolean add(Item e)
	{
		if (this.searchItem(e.getId()) == -1)
		{
			super.add(e);
			return true;
		}

		return false;

	}

	public void add(int index, Item element)
	{
		if (this.searchItem(element.getId()) == -1)
		{
			super.add(index, element);
		}

	}

	public void addFirst(Item e)
	{
		if (this.searchItem(e.getId()) == -1)
		{
			super.addFirst(e);
		}
	}

	public void addLast(Item e)
	{
		if (this.searchItem(e.getId()) == -1)
			super.addLast(e);
	}

	public boolean contains(Item e)
	{

		String id = e.getId();
		return (this.searchItem(id) != -1);
	}
	public String toString() {
		String aux=null;
		StringBuilder sb=new StringBuilder();
		if (this.size()==0) return null;
		
		for(int i = 0 ; i < this.size(); i++) {
			aux=this.get(i).toString();
			if (!aux.endsWith("0"))
			sb.append(aux);
			if (i<this.size()-1)
			sb.append('\n');
			
		}
		if (sb.length()<3) return null;
		return sb.toString();
		
	}
	public void clearDeleted() {
		this.searchItem("@$#");
	}
}
